/* Test2.cs -- 
   Copyright (C) 2008  Casey Marshall.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.  */


using System;
using System.Collections;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Threading;

using SexpSharp.Remoting;

namespace SexpSharp.Remoting.Tests
{
    public interface IHello3
    {
		[SexpMethodName("hello")]
        string HelloWorld();

        void CheckProtocolVersion(string s1, string s2, int v);
        Blob HelloBlob();
    }

    public class Hello3 : MarshalByRefObject, IHello3
    {
        const int protocol = 5;
		[SexpMethodName("hello")]
        public string HelloWorld()
        {
            return "Hello, World!";
        }

        public void CheckProtocolVersion(string s1, string s2, int v)
        {
            System.Console.WriteLine(s1 + " " + s2 + " " + v);
            if (v != protocol)
                throw new Exception("incompatible protocol");
        }

        public Blob blob = new Blob("Hello, World!");

        public Blob HelloBlob()
        {
            return blob;
        }
    }

    public class Blob : MarshalByRefObject
    {
        public string blob;
        public Blob(string b) { blob = b; }
        public string GetBlob() { return blob; }
    }

    public class Test3
    {
        public static void Main(string[] argv)
        {
            if (argv.Length > 0 && argv[0] == "client")
            {
                setupRemotingChannel();
                for (int i = 0; i < 1000; i++)
                    runRemote();
            }
            else
            {
                setupRemotingChannel();
                registerService();
                Thread.Sleep(1000000);
            }
        }

        static void setupRemotingChannel()
        {
            Hashtable props = new Hashtable();
            props["port"] = 6001;
			props["name"] = "RemotingTest3";
            SexpServerFormatterSinkProvider serv =
                new SexpServerFormatterSinkProvider();
            SexpClientFormatterSinkProvider cli =
                new SexpClientFormatterSinkProvider();
            TcpChannel channel = new TcpChannel(props, cli, serv);
            ChannelServices.RegisterChannel(channel, false);
        }

        static void registerService()
        {
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(Hello3),
                                                               "hello3",
                                                               WellKnownObjectMode.Singleton);
        }

        static void runRemote()
        {
            IHello3 hello = (IHello3) Activator.GetObject(typeof(IHello3),
                                                          "tcp://localhost:6001/hello3");
            Console.WriteLine(hello.HelloWorld());
            hello.CheckProtocolVersion(null, null, 5);
            Console.WriteLine(hello.HelloBlob());
            Console.WriteLine(hello.HelloBlob().GetBlob());
            Console.WriteLine(hello.HelloBlob().blob);
        }
    }
}